﻿using AutoMapper;
using HamcoWcs.Entitys.MainTaskBaks;
using HamcoWcs.Entitys.MainTasks;
using HamcoWcs.Models.ForeEndModels.UITasks;
using System;
using System.ComponentModel;

namespace HamcoWcs.Models.MainTasks
{
    public class MainTaskProfile : Profile
    {

        public MainTaskProfile()
        {
            CreateMap<MainTask, MainTaskDto>();
            //.ForMember(dest => dest.Extend, opt => opt.MapFrom(src => ExtendDto.Deserlize(src.Extend)));
            CreateMap<MainTaskDto, MainTask>();
            //.ForMember(dest => dest.Extend, opt => opt.MapFrom(src => src.Extend.ToJson()));

            CreateMap<MainTask, MainTaskBak>().
                ForMember(dest => dest.Id, opt => opt.Ignore()); 

            CreateMap<MainTask, UIMainTask>();
            CreateMap<MainTask, UIMainTaskTimeOut>();
            CreateMap<UIMainTaskTimeOut, MainTask>();
            CreateMap<MainTaskBak, UIMainTask>();

            CreateMap<MainTaskBak, MainTask>();
            CreateMap<MainTaskDto, MainTaskBak>().
                ForMember(dest => dest.Id, opt => opt.Ignore()); 
            CreateMap<MainTaskBak, MainTaskDto>();


            CreateMap<DateTime, string>().ConvertUsing<DateTimeToStringConverter>();
        }
    }

    public sealed class DateTimeToStringConverter : ITypeConverter<DateTime, string>
    {
        public string Convert(DateTime source, string destination, ResolutionContext context)
        {
            return source.ToString("G");
        }


    }
}
